import http from "../utils/request"
import {PageRequestType, ResponseType, PageResponseType} from "../types/Response";
import {AxiosResponse} from "axios";

const apiPrefix:string = "/goods";

export interface GoodType {
    // 主键
    id?:string|null|undefined
    // 一级类型
    primary_type_id?:string|null|undefined
    // 二级类型
    secondary_type_id?:string|null|undefined
    // 三级类型
    tertiary_type_id?:string|null|undefined
    // 商品名
    name?:string|null|undefined
    // 超简介
    less_intro?:string|null|undefined
    // 简介
    intro?:string|null|undefined
    // 详情
    details?:string|null|undefined
    // 生产商
    producer?:string|null|undefined
    // 主图片
    main_image?:string|null|undefined
    // 轮播图
    sub_images?:string|null|undefined
    // 规格列表(存为json)
    attribute_list?:string|null|undefined
    // 货币
    currency?:string|null|undefined
    // 原价(所有规格最低价,用于显示商品列表中价格)
    price?:number|null|undefined
    // 现价(所有规格最低价,用于显示折扣价格)
    current_price?:number|null|undefined
    // 库存数量
    stock?:number|null|undefined
    // 售卖状态(1正常售卖,0停止售卖)
    status?:number|null|undefined
    // 创建时间
    create_at?:any|null|undefined
    // 修改时间
    update_at?:any|null|undefined
}

export interface GoodPageSearchType extends PageRequestType, GoodType {

}


export async function addGood(data: GoodType): Promise<AxiosResponse<ResponseType<null>>> {
    return await http.request({
        data: data,
        url: apiPrefix,
        method: 'post'
    })
}

export async function deleteGoodById(id: string): Promise<AxiosResponse<ResponseType<null>>> {
    return await http.request({
        params: {
            id
        },
        url: apiPrefix+'/id',
        method: 'delete'
    })
}

export async function deleteGoodByIds(ids: string[]): Promise<AxiosResponse<ResponseType<null>>> {
    return await http.request({
        params: {
            ids
        },
        url: apiPrefix+'/batch',
        method: 'delete'
    })
}


export async function searchGoodById(id: string): Promise<AxiosResponse<ResponseType<any>>> {
    return await http.request({
        params: {
            id
        },
        url: apiPrefix+'/goodsInfo',
        method: 'get'
    })
}

export async function searchGoodByRandom(n:number): Promise<AxiosResponse<ResponseType<GoodType[]>>> {

    return await http.request({
        params: {
            n
        },
        url: apiPrefix+'/getRandomGoods',
        method: 'get'
    })
}
export async function searchGoodByRecommend(): Promise<AxiosResponse<ResponseType<GoodType[]>>> {

    return await http.request({
        url: apiPrefix+'/recommend',
        method: 'get'
    })
}
export async function searchGoodByType(typeId?:string,current:number=1,size:number=10,text?:string): Promise<AxiosResponse<ResponseType<PageResponseType<GoodType>>>> {
    return await http.request({
        params: {
            typeId:typeId,
            current,
            size,
            text
        },
        url: apiPrefix+'/filtrateByType',
        method: 'get'
    })
}

export async function searchGood(params?: GoodType): Promise<AxiosResponse<ResponseType<GoodType[]>>> {
    return await http.request({
        params: params,
        url: apiPrefix,
        method: 'get'
    })
}

export interface AttrEntry{
    key:string,
    value:string
}

export async function searchGoodSpecificationInfo(data:AttrEntry[]): Promise<AxiosResponse<ResponseType<GoodType[]>>> {
    return await http.request({
        data,
        url: '/goodsSpecification/specificationInfo',
        method: 'post'
    })
}

export async function searchGoodByPage(params: GoodPageSearchType): Promise<AxiosResponse<ResponseType<PageResponseType<GoodType>>>> {
    return await http.request({
        params: params,
        url: apiPrefix+'/page',
        method: 'get'
    })
}


export async function updateGoodById(data: GoodType): Promise<AxiosResponse<ResponseType<null>>> {
    return await http.request({
        data: data,
        url: apiPrefix,
        method: 'put'
    })
}

